//
//  RHComposeView.swift
//  微博新浪
//
//  Created by hezerong on 16/6/20.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

class RHComposeView: UITextView {
    
    override var font:UIFont?{
    
        didSet{
        
           placeHolderText.font = font
        
        }
     
        
    }
    
    //占位文字
    var placeHolder:String = "what's on your mind?"{
    
        didSet{
        
              placeHolderText.text = placeHolder
        }
    
    }
    
    //占位文字
    lazy var placeHolderText:UILabel = UILabel(text: "what's on your mind?", textColor: UIColor.lightGrayColor(), fontSize: 14)


    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setUPUI()
        
        
        //设置通知，监听textview是否有文字
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange:", name: UITextViewTextDidChangeNotification, object: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

extension RHComposeView{

    
    func setUPUI(){
    
       //设置垂直滑动弹簧效果
        alwaysBounceVertical = true
        
        //拖拽，隐藏键盘
        keyboardDismissMode = .OnDrag
        
        //添加占位文字
        addSubview(placeHolderText)
        
        //布局占位lable
        placeHolderText.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(8)
            make.left.equalTo(self).offset(8)
            make.right.equalTo(self).offset(-8)
            
        }
    }
    
    
 @objc private func textDidChange(notification:NSNotification){
    
     //如果输入框有值就隐藏placeholder里面的占位文字
    placeHolderText.text = hasText() ? "" : placeHolder
    
    }
}

